package cn.wizzer.app.web.modules.controllers.platform.user;

import cn.wizzer.app.user.modules.models.enums.PhoneCodeEnum;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import redis.clients.jedis.ScanParams;

import java.util.*;

/**
 * 用户验证码
 *
 * @author .
 */
@IocBean
@At("/platform/user/manager/verifyCode")
public class UserVerifyCodeController {
    private static final Log log = Logs.get();

    @Inject
    private RedisService redisService;

    @At("/")
    @Ok("beetl:/platform/user/manager/verifyCode/index.html")
    @RequiresPermissions("user.manager.verifyCode.list")
    public void index() {

    }


    /**
     * 查询用户验证码
     *
     * @return
     */
    @At()
    @Ok("json")
    @RequiresPermissions("user.manager.verifyCode.list")
    public Object data(@Param("keyWord") String keyWord,
                                 @Param("pageNumber") int pageNumber,
                                 @Param("pageSize") int pageSize) {
        ScanParams scanParams = new ScanParams();
        scanParams.match(PhoneCodeEnum.FIX + "*");
        scanParams.count(Integer.MAX_VALUE);
        List<String> keys = redisService.scan("0", scanParams).getResult();
        List<Map<String, String>> list = new ArrayList<>();
        String code,phonePrefix,phone,type;
        for (String key : keys) {
            // key:   code#phone#register:86&13811112222
            code = redisService.get(key);
            // 分割手机号
            phonePrefix = key.split("&")[0].split(":")[1];
            phone = key.split("&")[1];
            // 操作类型
            type = key.split(PhoneCodeEnum.FIX)[1].split(":")[0];
            // 筛选条件：次数为0的用户
            boolean cnd = true;
            // 按手机号搜索
            if (Strings.isNotBlank(keyWord)) {
                cnd = phone.contains(keyWord);
            }
            if (cnd) {
                HashMap<String, String> map = new HashMap<>();
                map.put("phonePrefix", phonePrefix);
                map.put("phone", phone);
                map.put("code", code);
                map.put("type", type);
                map.put("ttl", redisService.ttl(key) + "");
                list.add(map);
            }
        }
        // 分页
        int endIndex = pageNumber == 0 ? pageSize : pageNumber * pageSize;
        int count = list.size();
        int startIndex = endIndex - pageSize;
        list = list.subList(pageNumber == 0 ? 0 : startIndex, endIndex == 0 ? count : Math.min(endIndex, count));
        // 设置分页属性
        Pagination pagination = new Pagination();
        pagination.setTotalCount(count);
        pagination.setPageNo(pageNumber);
        pagination.setPageSize(pageSize);
        pagination.setList(list);
        return Result.success().addData(pagination);
    }


}
